import {ref, shallowRef, watch} from 'vue'
import {onReachBottom, onShow} from "@dcloudio/uni-app";
//定义options 类型
type Options = {
    params?: Record<string, any>,
}

export default function useQueryPage<T>(actionFun: Function, options: Options) {
    //定义分页所需参数，
    const loading = ref(false)
    const page = ref(1)
    const list = shallowRef<Array<T>>([])
    const errMsg = ref('')
    //分页查询操作
    const handleQueryPage = async () => {
        loading.value = true
        list.value = []
        const res = await actionFun({ page: page.value });
        if (res && !res.msg) {
            page.value = page.value + 1
            list.value = list.value.concat(res.data)
        }
        errMsg.value = res.msg
        loading.value = false
    }

    onShow(() => {
        page.value = 1;
        list.value = [];
        handleQueryPage();
    })
    // 滚动到底部
    onReachBottom(() => {
        console.log('ssss', onReachBottom)
        handleQueryPage()
    })

    return {page, list, handleQueryPage, loading, errMsg}
}
